let express = require("express");
let app = express();
let whitList = ["http://localhost:3000"]
app.use(function (req, res, next) {
    let origin = req.headers.origin;
    if (whitList.includes(origin)) {
        // 设置哪个源访问
        res.setHeader("Access-Control-Allow-Origin", origin);
        //允许哪个头 访问
        res.setHeader("Access-Control-Allow-Headers", "name");
        //允许哪个方法访问
        res.setHeader("Access-Control-Allow-Methods", "get");
        // 允许携带cookie
        res.setHeader("Access-Control-Allow-Credentials", true);
        //预请求 检测存活时间
        res.setHeader("Access-Control-Max-Age", 6);
        //允许返回设置的头
        res.setHeader("Access-Control-Expose-Headers", "name");//多个用逗号隔开
        // if(req.method==="OPTIONS"){
        //     res.send();//OPTIONS不做处理
        // }
    }
    next();

})

app.get('/getData', function (req, res) {
    console.log(req.headers)
    res.setHeader("name", "ss");
    res.end('我不是dragon');
})
//以当前目录作为静态文件目录
app.use(express.static(__dirname));
app.listen(4000)